package cn.com.fuhuitong.main.base

import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import cn.com.fuhuitong.base.BaseAbstractActivity
import cn.com.fuhuitong.base.BaseViewModel
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.FixLayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.base.EmptyContent
import cn.com.fuhuitong.base.EmptyViewAdapter
import cn.com.fuhuitong.entity.EmptyState
import cn.com.fuhuitong.entity.MainBean
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.ItemViewType
import cn.com.fuhuitong.main.dialog.LoadingDialog
import com.blankj.utilcode.util.LogUtils

abstract class ViewModeActivity<ViewModel : BaseViewModel> : BaseAbstractActivity() {


    lateinit var viewModel: ViewModel

    lateinit var emptyViewAdapter: EmptyViewAdapter

    abstract fun viewModel(): ViewModel

    override fun doBeforeInit() {
        super.doBeforeInit()
        viewModel = viewModel()
        initViewMode()
    }

    /**
     * 初始化 view mode
     * */
    open fun initViewMode() = Unit

    /**
     * 生成ViewModel
     */
    inline fun <reified VM : BaseViewModel> createViewModel(): VM =
        ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        )
            .get(VM::class.java)


    lateinit var delegateAdapter: DelegateAdapter
    fun initViewRecyclerAdapter(
        recyclerView: RecyclerView, viewTypes: Array<Int>, max: Int = 5
    ) {
//        view.isNestedScrollingEnabled = false

        val vLayoutManager: VirtualLayoutManager = VirtualLayoutManager(this).apply {
            setLayoutViewFactory { ImageView(this@ViewModeActivity) }
        }

        with(recyclerView) {
            setRecycledViewPool(RecyclerView.RecycledViewPool().apply {
                setMaxRecycledViews(ItemViewType.EMPTY, max)
                for (type: Int in viewTypes) {
                    setMaxRecycledViews(type, max)
                }
            })
            layoutManager = vLayoutManager
            adapter = DelegateAdapter(vLayoutManager, (false))
                .apply {
                    delegateAdapter = this
                }
        }

        delegateAdapter.addAdapter(
            EmptyViewAdapter(
                this, FixLayoutHelper(FixLayoutHelper.BOTTOM_LEFT, 0, 0).apply {
                    marginTop = 500
                }
            ).apply {
                emptyViewAdapter = this
            })
        emptyViewAdapter.setOnItemChildClickListener { view, _, mainBean ->
            if (view.id == R.id.text_empty_execute) {
                onLoadingEmptyView(mainBean.type)
            }
        }
    }

    open fun onLoadingEmptyView(state: EmptyState) = Unit

    /**
     * 显示加载布局
     * */
    fun showLoadEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.START
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 显示异常布局
     * */
    fun showErrorEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.FAILURE
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 显示空数据布局
     * */
    fun showNullEmptyView(emptyContent: EmptyContent = EmptyContent()) {
        val mainBean = MainBean<EmptyState, EmptyContent>(
            type = EmptyState.NULL
        )
        mainBean.data = emptyContent
        emptyViewAdapter.setAddDataNew(mutableListOf(mainBean))
    }

    /**
     * 删除空布局
     * */
    fun hideEmptyView() {
        emptyViewAdapter.data.clear()
        emptyViewAdapter.notifyDataSetChanged()
    }

    /**
     * 设置加载对话框
     * */
    private var loadingDialog: LoadingDialog? = null
    fun showLoadingView(content: String? = null) {
        if (loadingDialog == null) {
            loadingDialog = LoadingDialog(this)
        }
        loadingDialog?.showLoading(content)
    }

    /**
     * 删除加载对话框
     * */
    fun hideLoadingView() {
        loadingDialog?.dismiss()
    }

    /**
     * 处理不同数据状态时页面的显示
     */
    fun handlerResponseLoading(
        status: HttpResponseStatus.Status, errorTip: String, isLoading: Boolean = true
    ) {
        when (status) {
            HttpResponseStatus.Status.START -> if (isLoading) showLoadingView()
            HttpResponseStatus.Status.SUCCESS -> if (isLoading) hideLoadingView()
            HttpResponseStatus.Status.FAILURE -> {
                if (isLoading) hideLoadingView()
                toast(errorTip)
            }
        }
    }


    /**
     * 处理不同数据状态时页面的显示
     */
    fun handlerResponseEmpty(
        status: HttpResponseStatus.Status,
        message: String, length: Int = 0,
        swipe: SwipeRefreshLayout? = null,
        emptyContent: EmptyContent = EmptyContent()
    ) {
        when (status) {
            HttpResponseStatus.Status.START -> {
                if (length == 0) {
                    this.showLoadEmptyView()
                }
            }
            HttpResponseStatus.Status.SUCCESS -> {
                if (length == 0) {
                    this.showNullEmptyView(emptyContent)
                } else {
                    this.hideEmptyView()
                }
            }
            HttpResponseStatus.Status.FAILURE -> {
                if (length == 0) {
                    this.showErrorEmptyView(EmptyContent("重新加载", message))
                }
                swipe?.let {
                    it.isRefreshing = false
                }
                toast(message)
            }
        }
    }

}